package nts

import (
	"net"
)

func init() {
	getLocalhosts()
}

var localhosts []string

func getLocalhosts() {
	ifaces, _ := net.Interfaces()
	//get local nets from
	for _, iface := range ifaces {
		if up, loopback := net.FlagUp&iface.Flags, net.FlagLoopback&iface.Flags; 0 != up && 0 == loopback {
			if addrs, err := iface.Addrs(); nil == err {
				for _, addr := range addrs {
					if ipNet, ok := addr.(*net.IPNet); ok && !ipNet.IP.IsLoopback() {
						if ipNet.IP.To4() != nil {
							ip := ipNet.IP.String()
							localhosts = append(localhosts, ip)
						}
					}
				}
			}
		}
	}
}

func isLocalhost(ip string) bool {
	for _, localhost := range localhosts {
		if ip == localhost {
			return true
		}
	}
	return false
}
